package com.emcom.gotour.mvc.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.emcom.gotour.mvc.model.TourContentModel;
import com.emcom.gotour.mvc.view.GlobalView;
import com.emcom.gotour.mvc.view.InsertTourContentView;
import com.emcom.gotour.mvc.view.NavigationView;
import com.emcom.gotour.mvc.view.TourContentView;
import com.emcom.gotour.po.TourContent;
import com.emcom.gotour.service.TourCategoryService;
import com.emcom.gotour.service.TourManageService;
import com.emcom.gotour.utils.LocaleUtils;

@Controller
public class InsertTourController {

	@Autowired
	private TourManageService tourManageService;

	@Autowired
	private TourCategoryService tourCategoryService;

	@Autowired
	private MessageSource messageSource;

	@RequestMapping(value={"/admin/insert_tour.html"})
	public String insertTour(HttpServletRequest request) {
		InsertTourContentView view = new InsertTourContentView();
		view.setCategoryList(tourCategoryService.queryCategory());
		view.setTitle(messageSource.getMessage("tour.insert.title", null, LocaleUtils.getLocale(request)));
		view.setNavigationView(new NavigationView(request, messageSource));
		view.setGlobalView(new GlobalView(request, messageSource));
		view.setTourContentView(new TourContentView(request, messageSource));
		request.setAttribute("view", view);
		return "/admin/insert_tour";
	}

	@RequestMapping(value={"/admin/insert_tour_action.html"})
	public String insertTourAction(HttpServletRequest request) {
		TourContent tourContent = TourContentModel.getObject(request);
		tourManageService.saveTour(tourContent);
		return insertTour(request);
	}

}
